﻿<#
.SYNOPSIS  
    Removes User Profile Service Applications.  
.DESCRIPTION  
    
.NOTES
    
.LINK  
    http://7up.codeplex.com 
#>

Write-Host ("   Removing Profile Service Applications ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

foreach($psa in $config.Settings.ServiceApplications.ProfileServiceApplications.ProfileServiceApplication)
{
	if($psa -ne $null)
	{
		#remove proxy
		$proxyName = $psa.Name + " Proxy"
		$sap = Get-SPServiceApplicationProxy | Where-Object {$_.DisplayName -eq $proxyName}
		if($sap -eq $null)
		{
			Write-Host ("      Skipping '{0}' because it does not exist." -f $proxyName)
		}
		else
		{	
			Write-Host ("      Removing '{0}'..." -f $proxyName)
			Remove-SPServiceApplicationProxy $sap -Confirm:$false -RemoveData
		}
		
		#remove service app
		$sa = Get-SPServiceApplication | Where-Object {$_.DisplayName -eq $psa.Name}
		if($sa -eq $null)
		{
			Write-Host ("      Skipping '{0}' because it does not exist." -f $psa.Name)
		}
		else
		{	
			Write-Host ("      Removing '{0}'..." -f $psa.Name)
			Remove-SPServiceApplication $sa -Confirm:$false -RemoveData
		}
	}
}

